package com.app.lcl.test;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.boot.registry.StandardServiceRegistry;
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
import org.hibernate.cfg.Configuration;

import com.app.lcl.entities.News;

public class testHibernate {

	public static void main(String[] args)throws Exception {
		/*实例化Configuration,并加载hibernate的配置文件*/
		Configuration conf = new Configuration().configure();
		StandardServiceRegistryBuilder srb = new StandardServiceRegistryBuilder().applySettings(conf.getProperties());
		StandardServiceRegistry sr = srb.build();
		SessionFactory sf = conf.buildSessionFactory(sr);
		/*创建session*/
		Session sess = sf.openSession();
		/*开始事务*/
		Transaction tx = sess.beginTransaction();
		//创建新闻实例
		News n = new News();
		//为实例化对象赋值
		n.setTitle("新闻标题1");
		n.setContent("新闻内容4");
		sess.save(n);//insert obj
		tx.commit();//提交对象
		sess.close();//关闭会话
		sf.close();//关闭工厂
	}
}
